package ui;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sxpjyy.monkey.mobilenurse.R;

import java.util.Collections;
import java.util.List;

import adapters.SectionListAdapter;
import mytools.MyBaseActivity;
import objects.Section;
import objects.User;
import service.WebService;

public class SectionsListActivity extends MyBaseActivity implements SwipeRefreshLayout.OnRefreshListener, AdapterView.OnItemClickListener {

    private SwipeRefreshLayout swipeRefreshLayout;
    private ListView listView;
    private SectionListAdapter sectionsAdapter;
    private List<Section> mSectionList;

    private String YGDM = null;
    private String mSectionsResult = null;
    private static ProgressDialog progressDialog = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_section_list);
        onBindUI();

        sectionsAdapter = new SectionListAdapter(this);
        listView.setAdapter(sectionsAdapter);
        listView.setOnItemClickListener(this);
        swipeRefreshLayout.setColorSchemeResources(R.color.holo_green_dark, R.color.holo_green_light, R.color.holo_orange_light, R.color.holo_red_light);
        swipeRefreshLayout.setOnRefreshListener(this);
    }

    private void onBindUI() {
        swipeRefreshLayout = findViewById(R.id.swipe_layout);
        listView = findViewById(R.id.section_list);
    }

    @Override
    protected void onResume() {
        super.onResume();
        User user = getMyApplication().getUser();
        if (user != null) {
            YGDM = user.getYGDM();
            showProgressBar(this);
            new MySectionTask().execute();
        } else {
            Toast.makeText(this, "获取用户数据失败，请重新登陆！", Toast.LENGTH_SHORT).show();
//            startActivity(new Intent(this, LoginActivity.class));
        }
    }

    @Override
    public void onRefresh() {
        new MySectionTask().execute();
    }

    //显示等待对话框
    private static void showProgressBar(Context context) {
        progressDialog = new ProgressDialog(context);
        progressDialog.setMessage("正在获取信息。。。");
        progressDialog.setCancelable(false);
        progressDialog.show();
    }

    //取消等待对话框
    private static void cancelProgressBar() {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.cancel();
            progressDialog = null;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
        switch (adapterView.getId()) {
            case R.id.section_list:
                Section section = mSectionList.get(position);
                if (section != null) {
                    sectionsAdapter.setWhich(position);
                    sectionsAdapter.notifyDataSetChanged();
                    Intent intent = new Intent(SectionsListActivity.this, PatientsListActivity.class);
                    getMyApplication().setSection(section);
                    startActivity(intent);
                }
                break;
            default:
                break;
        }
    }

    private class MySectionTask extends AsyncTask<Void, Void, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            listView.setEnabled(false);
        }

        @Override
        protected String doInBackground(Void... voids) {
            WebService sectionsWebService = new WebService(SectionsListActivity.this);
            sectionsWebService.setMETHOD_NAME("getSectionsList");
            if (YGDM.equals("")) {
                return "员工代码为空！";
            } else {
                String result;
                String[] Parms = {YGDM};
                sectionsWebService.setPARMS(Parms);
                result = sectionsWebService.getServerData();
                if (result == null || result.equals("null") || result.equals("") || result.equals("[]")) {
                    return "获取科室列表失败！";
                } else {
                    if (!result.equals(mSectionsResult)) {
                        mSectionsResult = result;
                        Gson gson = new Gson();
                        mSectionList = gson.fromJson(mSectionsResult, new TypeToken<List<Section>>() {
                        }.getType());
                        if (mSectionList != null && mSectionList.size() > 0) {
                            Collections.sort(mSectionList, (arg0, arg1) -> arg0.getKSDM().compareTo(arg1.getKSDM()));
                            return null;
                        } else {
                            return "获取科室列表为空！";
                        }
                    } else {
                        return "数据未发生变化";
                    }
                }
            }
        }

        @Override
        protected void onPostExecute(String message) {
            super.onPostExecute(message);
            cancelProgressBar();
            if (swipeRefreshLayout.isNestedScrollingEnabled()) {
                swipeRefreshLayout.setRefreshing(false);
            }
            listView.setEnabled(true);
            if (message != null) {
                Toast.makeText(SectionsListActivity.this, message, Toast.LENGTH_SHORT).show();
            } else {
                sectionsAdapter.setList(mSectionList);
                sectionsAdapter.notifyDataSetChanged();
            }
        }

        @Override
        protected void onCancelled() {
            super.onCancelled();
            cancelProgressBar();
        }
    }

}
